package com.yinda.dt.produce.module.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xcgame.pp.activity.PicturePickerActivity;
import com.yinda.dt.produce.R;
import com.yinda.dt.produce.app.DemoActivity;
import com.yinda.dt.produce.app.DemoConfig;
import com.yinda.dt.produce.app.MyResponseCallback;
import com.yinda.dt.produce.app.MyUploadResponseCallback;
import com.yinda.dt.produce.module.login.LoginActivity;
import com.yinda.dt.produce.view.SupportEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class AdviseActivity extends DemoActivity implements View.OnClickListener {

    private LinearLayout ll_left;

    private SupportEditText set_content;
    private TextView tv_textHint;
    private Button b_submit;
    private ImageView iv_picture;
    private LinearLayout ll_add;
    private String picturePath = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advise);

        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_left.setOnClickListener(this);
        set_content = (SupportEditText) findViewById(R.id.set_content);
        tv_textHint = (TextView) findViewById(R.id.tv_textHint);
        b_submit = (Button) findViewById(R.id.b_submit);
        b_submit.setOnClickListener(this);
        iv_picture = (ImageView) findViewById(R.id.iv_picture);
        ll_add = (LinearLayout) findViewById(R.id.ll_add);
        ll_add.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_left) {
            finish();
        }
        if (id == R.id.ll_add) {

            PicturePickerActivity.start(AdviseActivity.this, 1, 12, 2, picturePath);
        }
        if (id == R.id.b_submit) {
            if (picturePath.equals("")) {
                submit(null);
            } else {
                upload(picturePath);
            }

        }
    }

    private void upload(String picturePath) {

        try {
            String url = getString(R.string.defaultURL) + "/phone1_0!upload.action";
            JSONObject jsonParams = new JSONObject();
            jsonParams.put("imgDataFileName", "DT_" + System.currentTimeMillis());

            uploadFile(url, null, new File(picturePath), new MyUploadResponseCallback(loadingDialog) {
                @Override
                public void onSuccess(JSONObject jsonResult) {
                    super.onSuccess(jsonResult);

                    try {
                        JSONObject jsonHead = jsonResult.getJSONObject("head");
                        int status = jsonHead.getInt("status");
                        String info = jsonHead.getString("info");
                        if (status == 1) {
                            JSONObject jsonData = jsonResult.getJSONObject("data");
                            submit(jsonData.getString("url"));
                        } else {
                            if (info.contains("key无效") || info.toLowerCase().contains("loginout") || info.contains("未登录")) {
                                DemoConfig.getInstance(getContext()).setKey("");
                                DemoConfig.getInstance(getContext()).setIsLogin(false);
                                showToast("请重新登录");
                                Intent intent = new Intent(getContext(), LoginActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            } else {
                                showToast(info);
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void submit(String pictureUrl) {

        try {
            String url = getString(R.string.defaultURL) + "/phone1_0!Feedback.action";
            JSONObject jsonParams = new JSONObject();
            jsonParams.put("remark", set_content.getText().toString());
            jsonParams.put("tel", DemoConfig.getInstance(this).getTel());
            jsonParams.put("name", DemoConfig.getInstance(this).getName());
            jsonParams.put("uid", DemoConfig.getInstance(this).getUid());
            jsonParams.put("encrypt", true);
            if (pictureUrl != null) {
                jsonParams.put("img", pictureUrl);
            } else {
                jsonParams.put("img", "");
            }
            post(url, jsonParams, new MyResponseCallback(loadingDialog) {
                @Override
                public void onSuccess(JSONObject jsonResult) {
                    super.onSuccess(jsonResult);

                    try {

                        JSONObject jsonHead = jsonResult.getJSONObject("head");
                        int status = jsonHead.getInt("status");
                        String info = jsonHead.getString("info");
                        if (status == 1) {
                            showToast("提交成功！");
                            finish();
                        } else {
                            if (info.contains("key无效") || info.toLowerCase().contains("loginout") || info.contains("未登录")) {
                                DemoConfig.getInstance(getContext()).setKey("");
                                DemoConfig.getInstance(getContext()).setIsLogin(false);
                                showToast("请重新登录");
                                Intent intent = new Intent(getContext(), LoginActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            } else {
                                showToast(info);
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == PicturePickerActivity.TAKE_PICTURE && data != null) {
            picturePath = data.getStringExtra("picturePath");
            Glide.with(this).load(picturePath).asBitmap().fitCenter().into(iv_picture);
        }
        if (requestCode == 1 && resultCode == PicturePickerActivity.TAKE_ALBUM && data != null) {
            picturePath = data.getStringExtra("picturePath");
            Glide.with(this).load(picturePath).asBitmap().fitCenter().into(iv_picture);
        }
        if (requestCode == 1 && resultCode == PicturePickerActivity.DELETE_PICTURE) {
            picturePath = "";
            iv_picture.setImageBitmap(null);
        }
    }
}
